package com.fruit.service.sysmsg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fruit.mapper.sysmsg.SysMessageMapper;
import com.fruit.pojo.user.sysmsg.SysMessage;
import com.fruit.util.DateUtil;
import com.fruit.util.Exceptions;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

/**
 * 站内消息Service
 * 
 * @author Administrator
 *
 */
@Service("sysMessageServiceImpl")
public class SysMessageServiceImpl implements SysMessageService {
	protected Logger logger = LoggerFactory.getLogger(SysMessageServiceImpl.class);
	@Autowired
	private SysMessageMapper sysMessageMapper;

	@Override
	public ResultInfo deleteByIds(String ids) {
		logger.info("【消息删除】---【开始】---ids={}", ids);
		ResultInfo result=null;
		try {
			if(StringUtils.isEmpty(ids)){
				logger.info("【消息删除】---【参数校验】---{}","传入参数ids不能为空!");
				result=ResultUtil.initResult(ResultInfo.ERROR, "delparam_error", "传入参数ids不能为空!");
			}else{
				sysMessageMapper.deleteByIds(ids);
				logger.info("【消息删除】---【删除成功】---");
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "del_success", "操作成功!");
			}
			return result;
		} catch (Exception e) {
			logger.error("【消息删除】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "del_exception", "删除消息异常！", "", "01");
			return result;
		}finally {
			if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
				logger.info("【消息删除】---【异常事务回滚】---{}---{}",result.getCode(),result.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			logger.info("【消息删除】---【结束】---");
		}
		
	}

	@Override
	public ResultInfo insert(SysMessage record) {
		logger.info("【新增消息】---【开始】---");
		ResultInfo result=null;
		try {
			if(record==null){
				logger.info("【新增消息】---【参数校验】---{}","入参不能为null");
				result=ResultUtil.initResult(ResultInfo.ERROR, "insertparam_error", "入参不能为null");
			}else{
				logger.info("【新增消息】---【parmas】---{}", record.toString());
				sysMessageMapper.insert(record);
				logger.info("【新增消息】---【保存成功】---");
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "insert_success", "操作成功!");
			}
			return result;
		} catch (Exception e) {
			logger.error("【新增消息】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "insert_exception", "插入消息异常！", "", "01");
			return result;
		}finally {
			if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
				logger.info("【新增消息】---【异常事务回滚】---{}---{}",result.getCode(),result.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			logger.info("【新增消息】---【结束】---");
		}
	}

	@Override
	public ResultInfo selectByUserId(Map<String,Object> parma) {
		logger.info("【用户消息查询】---【开始】---parma="+parma);
		ResultInfo result=null;
		try {
			if(parma==null){
				logger.info("【用户消息查询】---【参数校验】---{}","入参不能为null");
				result=ResultUtil.initResult(ResultInfo.ERROR, "selparam_error", "入参不能为null");
			}else{
				List<SysMessage> list=sysMessageMapper.selectByUserId(parma);
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "sel_success", "操作成功!");
				result.setObj(list);
				logger.info("【用户消息查询】---【成功】---");
			}
			return result;
		} catch (Exception e) {
			logger.error("【用户消息查询】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "sel_exception", "查询消息异常！");
			return result;
		}finally {
			logger.info("【用户消息查询】---【结束】---");
		}
	}

	@Override
	public ResultInfo selectById(Long id) {
		logger.info("【消息查询】---【开始】---id="+id);
		ResultInfo result=null;
		try {
			if(id==null){
				logger.info("【消息查询】---【参数校验】---{}","入参消息id不能为null");
				result=ResultUtil.initResult(ResultInfo.ERROR, "selparam_error", "入参消息id不能为null");
			}else{
				SysMessage msg=sysMessageMapper.selectById(id);
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "sel_success", "操作成功!");
				result.setObj(msg);
				logger.info("【消息查询】---【成功】---");
			}
			return result;
		} catch (Exception e) {
			logger.error("【消息查询】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "sel_exception", "查询消息异常！");
			return result;
		}finally {
			logger.info("【消息查询】---【结束】---");
		}
	}

	@Override
	public ResultInfo updateByIds(String ids) {
		logger.info("【更新消息状态】---【开始】---ids={}",ids);
		ResultInfo result=null;
		try {
			if(StringUtils.isEmpty(ids)){
				logger.info("【更新消息状态】---【参数校验】---{}","入参不能为null");
				result=ResultUtil.initResult(ResultInfo.ERROR, "param_error", "入参不能为null");
			}else{
				Map<String,Object> params=new HashMap<String, Object>();
				params.put("ids", ids);//消息id
				params.put("state", "01");//已读
				params.put("update_time", DateUtil.getTime());//更新时间
				int i=sysMessageMapper.updateByIds(params);
				logger.info("【更新消息状态】---【更新成功】---");
				if(i>0){
					result=ResultUtil.initResult(ResultInfo.SUCCESS, "update_success", "操作成功!");
				}else {
					result=ResultUtil.initResult(ResultInfo.ERROR, "update_fail", "操作失败!");
				}
				
			}
			return result;
		} catch (Exception e) {
			logger.error("【更新消息状态】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "update_exception", "更新消息状态异常！", "", "01");
			return result;
		}finally {
			if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
				logger.info("【更新消息状态】---【异常事务回滚】---{}---{}",result.getCode(),result.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			logger.info("【更新消息状态】---【结束】---");
		}
	}

	@Override
	public ResultInfo deleteByUserId(Integer userId) {
		logger.info("【用户消息删除】---【开始】---userId={}", userId);
		ResultInfo result=null;
		try {
			if(userId==null){
				logger.info("【用户消息删除】---【参数校验】---{}","传入参数userId不能为空!");
				result=ResultUtil.initResult(ResultInfo.ERROR, "delparam_error", "传入参数userId不能为空!");
			}else{
				sysMessageMapper.deleteByUserId(userId);
				logger.info("【用户消息删除】---【删除成功】---");
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "del_success", "操作成功!");
			}
			return result;
		} catch (Exception e) {
			logger.error("【用户消息删除】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "del_exception", "删除消息异常！", "", "01");
			return result;
		}finally {
			if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
				logger.info("【用户消息删除】---【异常事务回滚】---{}---{}",result.getCode(),result.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			logger.info("【用户消息删除】---【结束】---");
		}
		
	}

	@Override
	public ResultInfo updateByUserId(Integer userId) {
		logger.info("【更新用户消息状态】---【开始】---userId={}",userId);
		ResultInfo result=null;
		try {
			if(userId==null){
				logger.info("【更新用户消息状态】---【参数校验】---{}","入参userId不能为null");
				result=ResultUtil.initResult(ResultInfo.ERROR, "param_error", "入参userId不能为null");
			}else{
				Map<String,Object> params=new HashMap<String, Object>();
				params.put("userId", userId);//用户id
				params.put("state", "01");//已读
				params.put("update_time", DateUtil.getTime());//更新时间
				sysMessageMapper.updateByUserId(params);
				logger.info("【更新用户消息状态】---【更新成功】---");
				result=ResultUtil.initResult(ResultInfo.SUCCESS, "insert_success", "操作成功!");
			}
			return result;
		} catch (Exception e) {
			logger.error("【更新用户消息状态】---【异常】---{}", Exceptions.getStackTraceAsString(e));
			result= ResultUtil.initResult(ResultInfo.ERROR, "insert_exception", "更新消息状态异常！", "", "01");
			return result;
		}finally {
			if (ResultInfo.ROLLBACK.equals(result.getIsRollBack())) {
				logger.info("【更新用户消息状态】---【异常事务回滚】---{}---{}",result.getCode(),result.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			logger.info("【更新用户消息状态】---【结束】---");
		}
	}
	
	

}
